<?php

namespace App\V3\V3_2_7\Controllers;

use App\V3\V3_2_7\Services\MaterielSpecTempService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class MaterielSpecTempContrller extends BaseController
{
    /**
     *添加
     * Author: zlf
     * Created at 2024/4/25 14:52
     * @param Request $request
     * @return void
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'temp_name' => 'required|max:50',//模板名称
            'master_spce_info' => [
                'required',
                'json',
            ],
        ], [
            'temp_name.required' => '模板名称，必有填写',
            'temp_name.max' => '模板名称,字符长度超过50！',
            'master_spce_info.required' => '主规格，必有填写！',
            'master_spce_info.json' => '主规格为json数据！',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        if(!empty($data['master_spce_info'])){//主规格数据
            $msg = SELF::validSpce($data['master_spce_info']);
            if(!empty($msg)) return $this->fail('', $msg);
        }
        if(!empty($data['sub_spce_info'])){//副规格数据
            $msg = SELF::validSpce($data['sub_spce_info'],'副');
            if(!empty($msg)) return $this->fail('', $msg);
        }
        return response()->json(MaterielSpecTempService::add($data));

    }

    /**
     *删除
     * Author: zlf
     * Created at 2024/4/25 14:53
     * @param Request $request
     * @return void
     */
    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',
        ], [
            'id.required' => '参数`id`主键缺失',
            'id.integer' => '参数`id`主键,必有为整数！',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $condition = [];
        $condition['id'] = $data['id'];
        $condition['main_id'] = $this->getMainUserId();
        return response()->json(MaterielSpecTempService::del($condition));


    }

    /**
     *修改
     * Author: zlf
     * Created at 2024/4/25 14:53
     * @param Request $request
     * @return void
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',
            'temp_name' => 'required|max:50',//模板名称
            'master_spce_info' => [
                'required',
                'json',
            ],
        ], [
            'id.required' => '参数`id`主键缺失',
            'id.integer' => '参数`id`主键,必有为整数！',
            'temp_name.required' => '模板名称，必有填写',
            'temp_name.max' => '模板名称,字符长度超过50！',
            'master_spce_info.required' => '主规格，必有填写！',
            'master_spce_info.json' => '主规格为json数据！',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }
        $data['user_id'] = $this->getUserId();
        if(!empty($data['master_spce_info'])){//主规格数据
            $msg = SELF::validSpce($data['master_spce_info']);
            if(!empty($msg)) return $this->fail('', $msg);
        }
        if(!empty($data['sub_spce_info'])){//副规格数据
            $msg = SELF::validSpce($data['sub_spce_info'],'副');
            if(!empty($msg)) return $this->fail('', $msg);
        }
        $condition = [];
        $condition['id'] = $data['id'];
        $condition['main_id'] = $this->getMainUserId();
        unset($data['id']);
        return response()->json(MaterielSpecTempService::edit($condition,$data));

    }

    /**
     *列表
     * Author: zlf
     * Created at 2024/4/25 14:53
     * @param Request $request
     * @return void
     */
    public function list(Request $request)
    {
        $idArr = [];
        $data = $request->all();
        $data['main_id'] = $this->getMainUserId();
        if(!empty($data['ids'])){
            $idArr = explode(',',$data['ids']);
        }
        return response()->json([
            'code'=>0,
            'msg'=>'success',
            'data'=>MaterielSpecTempService::list($data,$idArr)
        ]);

    }

    /**
     *验证规格数据
     * Author: zlf
     * Created at 2024/4/25 16:47
     * @param $data
     * @param $title
     * @return string|void
     */
    public static function validSpce($data,$title = '主')
    {
        $n = 0;
        $arr  = json_decode($data,true);
        if(is_array($arr)){
            foreach ($arr as $val){
                $n++;
                if(empty($val['spce_name'])){
                    return '第'.$n.'个'.$title.'规格名称，不能为空！';
                }
            }
        }else{
            return $title.'规格数据有误！';
        }
    }
}
